<?php


namespace app\common\validate;

class UsersValidate extends CommonValidate
{
    protected $rule = [
        'mobile_number' => 'require|mobile',
        'password'      => 'require|length:8,20|notContainsChinese',
        'old_password'  => 'require|length:8,20|notContainsChinese',
        'new_password'  => 'require|length:8,20|notContainsChinese',
        //'repassword'    => 'require|confirm:password',
    ];

    protected $message = [
        'mobile_number.require'           => '请输入手机号',
        'mobile_number.mobile'            => '手机号格式错误',
        'password.require'                => '请输入密码',
        'password.length'                 => '密码必须是8-20个字符',
        'password.notContainsChinese'     => '密码必须是8-20个字符',
        'old_password.require'            => '请输入旧密码',
        'old_password.length'             => '密码必须是8-20个字符',
        'old_password.notContainsChinese' => '密码必须是8-20个字符',
        'new_password.require'            => '请输入新密码',
        'new_password.length'             => '密码必须是8-20个字符',
        'new_password.notContainsChinese' => '密码必须是8-20个字符',
    ];

    protected $scene = [
        'login'           => ['mobile_number', 'password'],
        'register'        => [
            'mobile_number',
            'password',
        ],
        'putPassword' => ['old_password', 'new_password'],
    ];

    /**
     * 自定义验证：助记词
     * @param $mnemonic_word
     * @return bool|null
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:59
     */
    public function mnemonicWord($mnemonic_word): ?bool
    {
        try {
            $mnemonicWordArr = explode(',', $mnemonic_word);
            $mnemonicWordArr = array_unique($mnemonicWordArr);
            if (count($mnemonicWordArr) !== 12) {
                return false;
            }
            foreach ($mnemonicWordArr as $v) {
                if (preg_match("/^[a-zA-Z\s]+$/", $v)) {
                    continue;
                }
                return false;
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}